<template>
  <div class="app-container">

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline" >
      <el-form-item label="轮播图标题">
        <el-input v-model="queryBannner.title" placeholder="轮播图标题" />
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="queryBannner.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="queryBannner.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getByPage()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="title" label="轮播图名称" width="132" align="center"/>

      <el-table-column label="轮播图" width="500" align="center">
        <template slot-scope="scope">
        <div class="pic">
          <img
            :src="scope.row.imageUrl"
            alt="scope.row.linkUrl"
            width="100%"
            height="100%"
          />
        </div>
        </template>
      </el-table-column>

      <el-table-column prop="gmtCreate" label="添加时间" width="160" align="center"/>
      <el-table-column prop="gmtModified" label="修改时间" width="160" align="center"/>
      <el-table-column prop="sort" label="排序" width="60" align="center"/>

      <el-table-column label="操作" width="200" align="center" >
        <template slot-scope="scope">
          <router-link :to="'/banner/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >修改</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeBannnerById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="updatePageNum"
      @current-change="getByPage"
      :current-page="page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="limit"
      style="padding: 30px 0; text-align: center"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>


<script>
import bannerApi from "@/api/edu/banner/banner";

export default {
  data() {
    return {
      listLoading: true, // 是否显示loading信息
      list: null, // 数据列表
      total: 0, // 总记录数
      page: 1, // 页码
      limit: 10, // 每页记录数
      queryBannner: {},
    };
  },
  created() {
    this.getByPage();
  },
  methods: {
    updatePageNum(limit = 10) {
      this.limit = limit;
      console.log(limit);
      this.getByPage();
    },
    getByPage(page = 1) {
      this.listLoading = true;
      this.page = page;
      bannerApi
        .getByPage(this.page, this.limit, this.queryBannner)
        .then((response) => {
          console.log(response);
          this.list = response.data.items;
          this.total = response.data.total;
          this.listLoading = false;
          console.log(response);
        });
    },
    resetData() {
      this.queryBannner = {};
      this.getByPage();
    },
    removeBannnerById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          bannerApi.deleteById(id).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getByPage();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style scoped>
.pic {
  width: 100%;
  height: 170px;
  overflow: hidden;
  float: left;
}
</style>